package com.capsulode.excel.conversion.impl;

import com.capsulode.excel.conversion.Converter;
import com.capsulode.excel.exception.CellTypeNotExpectException;
import com.capsulode.excel.exception.ConversionFailException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

public class DoubleConverter implements Converter<Double> {

    @Override
    public Double convert(Cell cell) throws CellTypeNotExpectException {
        if (CellType.NUMERIC.equals(cell.getCellTypeEnum())
                || CellType.FORMULA.equals(cell.getCellTypeEnum())) {
            return cell.getNumericCellValue();
        }
        if (CellType.BLANK.equals(cell.getCellTypeEnum())) {
            return null;
        }
        if (CellType.STRING.equals(cell.getCellTypeEnum())) {
            String stringCellValue = cell.getStringCellValue().trim();
            if (stringCellValue.isEmpty()) {
                return null;
            }
            try {
                return Double.parseDouble(stringCellValue);
            } catch (NumberFormatException ex) {
                throw new CellTypeNotExpectException(cell.getRowIndex(), cell.getColumnIndex(), "无效的数字：" + cell, ex);
            }
        }
        throw new CellTypeNotExpectException(cell.getRowIndex(), cell.getColumnIndex());
    }

    @Override
    public void serialize(Object value, Cell cell) throws ConversionFailException, CellTypeNotExpectException {
        if (value instanceof Number) {
            double val = ((Number) value).doubleValue();
            cell.setCellValue(val);
            return;
        }
        if (value instanceof CharSequence) {
            double val = Double.parseDouble(value.toString());
            cell.setCellValue(val);
            return;
        }
        throw new CellTypeNotExpectException(cell.getRowIndex(), cell.getColumnIndex());
    }
}
